package com.maben.easyui.controller;

import com.alibaba.fastjson.JSON;
import com.maben.easyui.pojo.UcModule;
import com.maben.easyui.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("list")
    public List<Map<String, Object>> queryMenuListNoPage(String parentId) {
        final List<Map<String, Object>> result = new ArrayList<>();
        try {
            final List<UcModule> ucModules = menuService.queryMenuListNoPage(parentId);
            final List<UcModule> collect = ucModules.stream().filter(ucModule -> ucModule.getId() == Integer.parseInt(parentId)).collect(Collectors.toList());
            collect.forEach(ucModule -> {
                final Map<String, Object> node = new HashMap<>();
                final Long id = ucModule.getId();
                node.put("id", id);
                node.put("text", ucModule.getModule_name());
                node.put("state", "open");

                final List<UcModule> childrenUc = ucModules.stream().filter(ucModule1 -> ucModule1.getParent_id() == id).collect(Collectors.toList());
                if (!childrenUc.isEmpty()) {
                    node.put("state", "open");
                    final List<Map<String,Object>> children = new ArrayList<>();
                    childrenUc.forEach(uc->{
                        final HashMap<String, Object> map = new HashMap<String, Object>() {{
                            put("id", uc.getId());
                            put("text", uc.getModule_name());
                            put("state", "open");
                        }};
                        if (uc.getChildrenNum()>0) {
                            map.put("state", "closed");
                        }
                        children.add(map);
                    });
                    node.put("children",children);
                }
                result.add(node);
            });
            System.out.println(JSON.toJSONString(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("list2")
    public List<Map<String, Object>> queryMenuListNoPage2(String id) {
        final List<Map<String, Object>> result = new ArrayList<>();
        try {
            final List<UcModule> ucModules = menuService.queryMenuListNoPage(id);
            final List<UcModule> collect = ucModules.stream().filter(ucModule -> ucModule.getId() == Integer.parseInt(id)).collect(Collectors.toList());
            if (!collect.isEmpty()){
                final UcModule parentUcModule = collect.get(0);
                final Map<String, Object> node = new HashMap<>();
                final Long parentUcModuleId = parentUcModule.getId();
                node.put("id", parentUcModuleId);
                node.put("name", parentUcModule.getModule_name());
                node.put("state","closed");
                final List<UcModule> childrenUc = ucModules.stream().filter(ucModule1 -> ucModule1.getParent_id().equals(parentUcModuleId)).collect(Collectors.toList());
                if (!childrenUc.isEmpty()) {
                    node.put("state","open");
                    final List<Map<String,Object>> children = new ArrayList<>();
                    childrenUc.forEach(uc->{
                        final HashMap<String, Object> map = new HashMap<String, Object>() {{
                            put("id", uc.getId());
                            put("name", uc.getModule_name());
                            if (uc.getChildrenNum()>0){
                                put("state", "closed");
                            }else {
                                put("state", "open");
                            }
                        }};
                        children.add(map);
                    });
                    node.put("children",children);
                }
                result.add(node);
            }
            System.out.println(JSON.toJSONString(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
